package com.dwxt.community.admin.controller;

import com.dwxt.common.base.*;
import com.dwxt.community.aiequipment.aibin.entity.*;
import com.dwxt.community.aiequipment.aibin.service.*;
import com.dwxt.community.aiequipment.sellmachine.entity.*;
import com.dwxt.community.aiequipment.sellmachine.service.*;
import com.dwxt.community.entity.*;
import org.apache.commons.lang3.*;
import org.springframework.beans.factory.annotation.*;
import org.springframework.web.bind.annotation.*;

import javax.validation.*;

/**
 * @program: intelligentcity
 * @description: 售卖机 后台
 * @author
 * @create: 2020/05/18
 */
@RestController
@RequestMapping("/cms/admin/sellMachine")
public class SellMachineControllerAdmin {

    @Autowired
    private SellMachineService sellMachineService;


    /**
     * showdoc
     *
     * @return
     * @catalog 社区后台管理/售卖机
     * @title 售卖机列表
     * @param pageNum 非必须 Integer 当前页
     * @param pageSize 非必须 Integer 每页条数
     * @method get
     * @description 售卖机列表
     * @url /cms/admin/sellMachine/get
     */
    @RequestMapping("/get")
    public BaseResult get(@RequestParam(defaultValue = "1") Integer pageNum,
                          @RequestParam(defaultValue = "10") Integer pageSize,
                          Integer using,
                          String name) {
        return BaseResult.ok(sellMachineService.get(pageNum, pageSize,name,using));
    }


    /**
     * showdoc
     *
     * @return
     * @catalog 社区后台管理/售卖机
     * @title 售卖机货道列表
     * @param pageNum 非必须 Integer 当前页
     * @param pageSize 非必须 Integer 每页条数
     * @method get
     * @description 售卖机货道列表
     * @url /cms/admin/sellMachine/aisleGet
     */
    @RequestMapping("/aisleGet")
    public BaseResult aisleGet(@RequestParam(defaultValue = "1") Integer pageNum,
                          @RequestParam(defaultValue = "10") Integer pageSize) {
        return BaseResult.ok(sellMachineService.aisleGet(pageNum, pageSize));
    }

    /**
     * showdoc
     *
     * @return
     * @catalog 社区后台管理/售卖机
     * @title 根据兑换机id查询售卖机货道列表
     * @param pageNum 非必须 Integer 当前页
     * @param pageSize 非必须 Integer 每页条数
     * @param machineId 必须 String 兑换机id
     * @method get
     * @description 售卖机货道列表
     * @url /cms/admin/sellMachine/getAisleByMachineId
     */
    @RequestMapping("/getAisleByMachineId")
    public BaseResult aisleGet(@RequestParam(defaultValue = "1") Integer pageNum,
                               @RequestParam(defaultValue = "10") Integer pageSize,
                               @RequestParam String machineId) {
        return BaseResult.ok(sellMachineService.getAisleByMachineId(pageNum, pageSize,machineId));
    }

    /**
     * showdoc
     *
     * @return
     * @catalog 社区后台管理/售卖机
     * @title 根据货道id查询货道详情
     * @param aisleId 必须 String 货道id
     * @method get
     * @description 售卖机【货道】详情
     * @url /cms/admin/sellMachine/sellMachineAisleDetail
     */
    @RequestMapping("/sellMachineAisleDetail")
    public BaseResult sellMachineAisleDetail(@RequestParam String aisleId){
        if (StringUtils.isBlank(aisleId)) {
            return BaseResult.build(StatusCode.PARAMETER_WRONG);
        }
        SellMachineAisle machineAisle = sellMachineService.sellMachineAisleDetail(aisleId);
        return BaseResult.ok(machineAisle);
    }

    /**
     * showdoc
     *
     * @return
     * @catalog 社区后台管理/售卖机
     * @title 根据货道id清空货道的商品
     * @param aisleId 必须 String 货道id
     * @method get
     * @description 清空【货道】的商品
     * @url /cms/admin/sellMachine/emptyGoodsByAisleId
     */
    @RequestMapping("/emptyGoodsByAisleId")
    public BaseResult emptyGoodsByAisleId(@RequestParam String aisleId){
        if (StringUtils.isBlank(aisleId)) {
            return BaseResult.build(StatusCode.PARAMETER_WRONG);
        }
        return BaseResult.ok(sellMachineService.emptyGoodsByAisleId(aisleId));
    }

    /**
     * showdoc
     *
     * @return
     * @catalog 社区后台管理/智能售卖机
     * @title 售卖机详情
     * @method get
     * @param id 必须 String 售卖机id
     * @description 售卖机详情
     * @url /cms/admin/sellMachine/detail
     */
    @RequestMapping("/detail")
    public BaseResult detail(String id) {
        if (StringUtils.isBlank(id)) {
            return BaseResult.build(StatusCode.PARAMETER_WRONG);
        }
        return BaseResult.ok(sellMachineService.detail(id));
    }
    /**
     * showdoc
     *
     * @return
     * @catalog 社区后台管理/智能售卖机
     * @title 添加或者修改售卖机
     * @method post
     * @description 添加或者修改智能售卖机
     * @url /cms/admin/sellMachine/insertOrUpdate
     */
    @RequestMapping("/insertOrUpdate")
    public BaseResult insertOrUpdate(@RequestBody @Valid SellMachine sellMachine) {
        return sellMachineService.insertOrUpdate(sellMachine);
    }
    /**
     * showdoc
     *
     * @return
     * @catalog 社区后台管理/智能售卖机
     * @title 添加或者修改货道
     * @method post
     * @description 添加或者修改货道
     * @url /cms/admin/sellMachine/aisleInsertOrUpdate
     */
    @RequestMapping("/aisleInsertOrUpdate")
    public BaseResult aisleInsertOrUpdate(@RequestBody @Valid SellMachineAisle sellMachineAisle) {
        return BaseResult.ok(sellMachineService.aisleInsertOrUpdate(sellMachineAisle));
    }

    /**
     * showdoc
     *
     * @return {"status":200,"msg":"SUCCESS","data": null}
     * @catalog 社区后台管理/智能售卖机
     * @title 启用或者停用设备
     * @method post
     * @param id 必须 String 设备号
     * @description 启用或者停用设备
     * @url /cms/admin/sellMachine/useOrStop
     */
    @RequestMapping("/useOrStop")
    public BaseResult useOrStop(String id) {
        if (StringUtils.isBlank(id)){
            return BaseResult.build(StatusCode.PARAMETER_WRONG);
        }
        return sellMachineService.useOrStop(id);
    }

    /**
     * showdoc
     *
     * @return {"status":200,"msg":"SUCCESS","data": null}
     * @catalog 社区后台管理/智能售卖机
     * @title 启用或者停用商品
     * @method post
     * @param id 必须 String 商品ID
     * @description 启用或者停用商品
     * @url /cms/admin/sellMachine/goodsUseOrStop
     */
    @RequestMapping("/goodsUseOrStop")
    public BaseResult goodsUseOrStop(String id) {
        if (StringUtils.isBlank(id)){
            return BaseResult.build(StatusCode.PARAMETER_WRONG);
        }
        return sellMachineService.goodsUseOrStop(id);
    }

    /**
     * showdoc
     *
     * @return
     * @catalog 社区后台管理/智能售卖机
     * @title 添加或者修改售卖机商品
     * @method post
     * @description 添加或者修改智能售卖机商品
     * @url /cms/admin/sellMachine/goodsInsertOrUpdate
     */
    @RequestMapping("/goodsInsertOrUpdate")
    public BaseResult goodsInsertOrUpdate(@RequestBody @Valid SellMachineGoods sellMachineGoods) {
        Float goodsPrice = sellMachineGoods.getGoodsPrice();
        String strPrice = String.valueOf(goodsPrice);
        if (strPrice.contains(".")){
            if (strPrice.length() - strPrice.indexOf(".") > 2){
                return BaseResult.build(StatusCode.PRICE_ERROR);
            }
        }
        return BaseResult.ok(sellMachineService.goodsInsertOrUpdate(sellMachineGoods));
    }

    /**
     * showdoc
     *
     * @return
     * @catalog 社区后台管理/智能售卖机
     * @title 商品列表
     * @method GET
     * @description 商品列表
     * @url /cms/admin/sellMachine/goodsGet
     */
    @RequestMapping("/goodsGet")
    public BaseResult goodsGet(@RequestParam(defaultValue = "1") Integer pageNum,
                               @RequestParam(defaultValue = "10") Integer pageSize) {
        return BaseResult.ok(sellMachineService.goodsGet(pageNum,pageSize));
    }

    /**
     * showdoc
     *
     * @return
     * @catalog 社区后台管理/智能售卖机
     * @title 获取所有启用的商品列表(不带分页)
     * @method GET
     * @description 商品列表
     * @url /cms/admin/sellMachine/allGoods
     */
    @RequestMapping("/allGoods")
    public BaseResult goodsGet() {
        return BaseResult.ok(sellMachineService.goodsAllGet());
    }

    /**
     * showdoc
     *
     * @return
     * @catalog 社区后台管理/智能售卖机
     * @title 商品详情
     * @method GET
     * @description 商品详情
     * @url /cms/admin/sellMachine/goodsDetail
     */
    @RequestMapping("/goodsDetail")
    public BaseResult goodsDetail(String id) {
        return BaseResult.ok(sellMachineService.goodsDetail(id));
    }

    /**
     * showdoc
     *
     * @return
     * @catalog 社区后台管理/智能售卖机
     * @title 商品销售记录
     * @method GET
     * @description 商品销售记录
     * @url /cms/admin/sellMachine/goodsSellRecord
     */
    @RequestMapping("/goodsSellRecord")
    public BaseResult goodsDetail(@RequestParam(defaultValue = "1") Integer pageNum,
                                  @RequestParam(defaultValue = "10") Integer pageSize) {
        return BaseResult.ok(sellMachineService.goodsSellRecord(pageNum,pageSize));
    }


}
